package com.design.android.personnel.ui;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import com.design.android.personnel.AppContext;
import com.design.android.personnel.R;
import com.design.android.personnel.bean.LoggedInUser;
import com.design.android.personnel.bean.User;
import com.design.android.personnel.database.UserOperate;
import com.design.android.personnel.utils.Md5Utils;


public class EditPersonalActivity extends AppCompatActivity {

    private EditText usernameEditText, phoneEditText;
    private EditText etOldPwd, etNewPwd, etNewPwdSure;
    private Spinner spinner;
    private Button btChangedPwd, cancelButton, confirmButton;
    private LinearLayout linearLayout;

    private UserOperate userOperate;

    // 当前登录的用户信息
    private User user = AppContext.getLoggedInUser().getUser();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_personal);

        userOperate = new UserOperate(this);
        initID();
        initView();

        btChangedPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i = linearLayout.getVisibility();
                if (i == View.VISIBLE) {
                    linearLayout.setVisibility(View.GONE);
                } else if (i == View.GONE) {
                    linearLayout.setVisibility(View.VISIBLE);
                }
            }
        });

        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                update();
            }
        });

        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void update() {
        String username = usernameEditText.getText().toString();
        String sex = spinner.getSelectedItem().toString();
        String oldUserPwd = etOldPwd.getText().toString();
        String newUserPwd = etNewPwd.getText().toString();
        String newUserPwdSure = etNewPwdSure.getText().toString();

        if (username.isEmpty()) {
            usernameEditText.requestFocus();
            usernameEditText.setError("姓名不能为空!");
            return;
        }

        if (linearLayout.getVisibility() == View.VISIBLE) {
            if (oldUserPwd.isEmpty()) {
                etOldPwd.requestFocus();
                etOldPwd.setError("原密码不能为空!");
                return;
            }

            String salt = user.getSalt();
            if (!user.getPassword().equals(Md5Utils.hash(oldUserPwd.concat(salt)))) {
                etOldPwd.requestFocus();
                etOldPwd.setError("原密码错误!");
                return;
            }

            if (newUserPwd.isEmpty()) {
                etNewPwd.requestFocus();
                etNewPwd.setError("新密码不能为空!");
                return;
            }
            if (newUserPwdSure.isEmpty()) {
                etNewPwdSure.requestFocus();
                etNewPwdSure.setError("匹配密码不能为空!");
                return;
            }
            if (!newUserPwd.equals(newUserPwdSure)) {
                etNewPwdSure.requestFocus();
                etNewPwdSure.setError("密码不匹配!");
                return;
            }
            user.setPassword(Md5Utils.hash(newUserPwdSure.concat(salt)));
        }

        user.setUsername(username);
        user.setSex(sex);

        userOperate.update(user);

        LoggedInUser loggedInUser = AppContext.getLoggedInUser();
        loggedInUser.setUser(user);
        AppContext.setLoggedInUser(loggedInUser);

        Toast.makeText(getApplicationContext(), "修改成功", Toast.LENGTH_SHORT).show();

        // 短暂延时后返回上个页面
        new Handler().postDelayed(new Runnable(){
            @Override
            public void run(){
                finish();
            }
        },2000);
    }

    private void initID() {
        usernameEditText = findViewById(R.id.username);

        phoneEditText = findViewById(R.id.phone);
        spinner = findViewById(R.id.personal_edit_sex);

        btChangedPwd = findViewById(R.id.personal_edit_bt);
        linearLayout = findViewById(R.id.personal_edit_layout);

        etOldPwd = findViewById(R.id.personal_edit_oldPwd);
        etNewPwd = findViewById(R.id.personal_edit_newPwd);
        etNewPwdSure = findViewById(R.id.personal_edit_newPwdSure);

        btChangedPwd = findViewById(R.id.personal_edit_bt);
        cancelButton = findViewById(R.id.cancel);
        confirmButton = findViewById(R.id.confirm);
    }

    private void initView() {
        usernameEditText.setText(user.getUsername());
        phoneEditText.setText(user.getPhone());

        spinner.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
                new String[]{"男", "女"}));
        if (user.getSex().equals("男")) {
            spinner.setSelection(0);
        } else if (user.getSex().equals("女")) {
            spinner.setSelection(1);
        }
        linearLayout.setVisibility(View.GONE);
    }
}
